#! /bin/sh

PROJECT_NAME=zbp-basedata-server

CURRENT_DIR=$(
    cd "$(dirname "$0")"
    pwd
)

# 进入部署目录
cd ${CURRENT_DIR}

echo "检测'$PROJECT_NAME'.jar文件是否存在"
if [ -f "$PROJECT_NAME.jar" ]; then
    echo "文件存在,继续执行"
  else
    echo "jar文件不存在,安装失败."
    exit
fi

cd bak
echo "清理多余备份文件"
number=7
success=0
while (success=0)
do
delfile=`ls -l -crt $PROJECT_NAME.jar.* | awk '{print $9 }' | head -1`
count=`ls -l -crt $PROJECT_NAME.jar.* | awk '{print $9 }' | wc -l`
if [ $count -gt $number ]; then
   rm $delfile
   echo "删除最早生成的备份文件: $delfile"
else
   success=1
   break;
fi
done

# 回到项目根目录
cd ..
echo "使用docker-compose进行构建新的容器并启动"
# docker-compose --env-file ${CURRENT_DIR}/config.env up --build -d
docker-compose up --build -d

TAG=$(date "+%Y%m%d%H%M")
echo "备份jar文件"
if [ -f "$PROJECT_NAME.jar" ]; then
    mv ./$PROJECT_NAME.jar ./bak/$PROJECT_NAME.jar.$TAG
  else
    echo "当前是首次安装，跳过备份。"
    exit
fi

echo "$PROJECT_NAME安装完成"
